#include "TextContentHandler.h"
#include "CXmlDesParser.h"
#include "UIControlDes.h"
#include "TextConverter.h"

CTextContentHandler::CTextContentHandler(LinkTemplate* aLink)
{
	iTextBlockTemplate = aLink;
}

CTextContentHandler::~CTextContentHandler()
{
	
	iTextStyleStack.Close();
}

void CTextContentHandler::OnStartElementL( const RTagInfo& aElement,
        const RAttributeArray& aAttributes, TInt aErrorCode )
    {
    if ( KErrNone == aErrorCode )
        {
        TextStyle* textStyle = NULL;
        if(aElement.LocalName().DesC().Compare(KTextStyle)==0)
			{
			textStyle = new(ELeave) TextStyle;
			iTextStyleStack.PushL(textStyle);
			}
        
        for(TInt i = 0; i < aAttributes.Count(); i++)
			{
			const RAttribute& attr = aAttributes[i];
			textStyle->SetPropertyL(attr.Attribute().LocalName().DesC(),
								attr.Value().DesC());
			}
        }
    else
        {
        // Do something if error happens.
        }
    }
 
void CTextContentHandler::OnEndElementL( const RTagInfo &aElement, TInt aErrorCode )
    {
    if ( KErrNone == aErrorCode )
        {
        if(aElement.LocalName().DesC().Compare(KTextStyle)==0)
        	{
        	iTextStyleStack.Pop();
        	}
        }
    else
        {
        // Do something if error happens.
        }
    }
 
void CTextContentHandler::OnContentL( const TDesC8 &aBytes, TInt aErrorCode )
    {
    if ( KErrNone == aErrorCode )
        {
        // aBytes stored the value of the parsed contents.
//        HBufC* pBuf = HBufC::NewL(aBytes.Length());
//        CleanupStack::PushL(pBuf);
//        TPtr ptr = pBuf->Des();
//        ptr.Copy(aBytes);
        TInt tt = aBytes.Length();
        HBufC* pBuf = TextConverter::ConvertUTF8ToUnicode(aBytes);
        CleanupStack::PushL(pBuf);
        CTextBlock* pBlock = CTextBlock::NewL(*pBuf);
        
        TextStyle* textStyle = iTextStyleStack.Top();
        if(textStyle)
        	{
        	pBlock->iTextStyle = textStyle;
        	pBlock->iTextStyle->AddRef();
        	}
        
        iTextBlockTemplate->PushL(pBlock);
        
        CleanupStack::PopAndDestroy(1);
        }
    else
        {
        // Display error messages here.
        }
    }

CTextContentHandler* CTextContentHandler::NewLC(LinkTemplate* aLink)
	{
	CTextContentHandler* self = new(ELeave) CTextContentHandler(aLink);
	CleanupStack::PushL(self);
	self->ConstructL(IXmlParserEx::EXmlParser_Des,*self);
	
	return self;
	}

CTextContentHandler* CTextContentHandler::NewL(LinkTemplate* aLink)
	{
	CTextContentHandler* self = CTextContentHandler::NewLC(aLink);
	CleanupStack::Pop(self);
	
	return self;
	}

void CTextContentHandler::ParseXmlL(const TDesC& aDes)
	{
	_LIT(KTextHeader,"<style font='Annotation' font-size='normal' "
			"color='#000000' backcolor='#FFFFFF' bold='false' italic='false'>");
	_LIT(KTextRear,"</style>");
	
	HBufC* pBuf = HBufC::NewL(KTextHeader().Length()+aDes.Length()+KTextRear().Length());
	CleanupStack::PushL(pBuf);
	TPtr ptr = pBuf->Des();
	ptr.Copy(KTextHeader);
	ptr.Append(aDes);
	ptr.Append(KTextRear);
	
	XmlContentHandler::ParseXmlL(*pBuf);
	
	CleanupStack::PopAndDestroy(pBuf);
	}